package dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import bean.Log;
import bean.Mail;
import bean.MailResume;
import bean.User;
import utils.DBCPUtil;

public class MailManagerDaoImpl implements MailManagerDao {
	QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource());
	/**
	 * 得到对应用户未读邮件的简述
	 * @return
	 */
	@Override
	public List<MailResume> getUnwatchedMail(String user_id) {
		// TODO Auto-generated method stub
		List<MailResume> unwatchmails=new ArrayList<MailResume>();
		try {
			unwatchmails=qr.query("select * from  mail_of_user where user_id=? and watched=0", new BeanListHandler<>(MailResume.class),user_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return unwatchmails;
	}
	/**
	 * 得到对应用户的所有邮件的简述
	 * @return
	 */
	@Override
	public List<MailResume> getAllMail(String user_id) {
		// TODO Auto-generated method stub
		List<MailResume> unwatchmails=new ArrayList<MailResume>();
		try {
			unwatchmails=qr.query("select * from  mail_of_user where user_id=?", new BeanListHandler<>(MailResume.class),user_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return unwatchmails;
	}
	/**
	 * 得到对应id的详细邮件内容
	 * @param id
	 * @return
	 */
	@Override
	public Mail getMail(int id) {
		// TODO Auto-generated method stub
		Mail mail=null;
		try {
			mail=qr.query("select * from mail where mail_id=?", new BeanHandler<>(Mail.class),id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mail;
	}
	/**
	 * 将对应用户的邮件的相应邮件设为已读
	 * @param id
	 * @return
	 */
	@Override
	public boolean watchedMail(String user_id, int mail_id) {
		// TODO Auto-generated method stub
		try {
			qr.update("update mail_of_user set watched=1 where user_id=? and mail_id=?", user_id,mail_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 删除对应用户的相应邮件
	 * @param id
	 * @return
	 */
	@Override
	public boolean deleteMail(String user_id, int mail_id) {
		// TODO Auto-generated method stub
		try {
			qr.update("delete from mail_of_user where user_id=? and mail_id=?", user_id,mail_id);
			qr.update("delete from mail where mail_id =? and (( select count(*) from mail_of_user where mail_id = ?) = 0)",mail_id,mail_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 将mail的内容加入到数据库中
	 * @param mail
	 * @return
	 */
	@Override
	public Mail addMail(Mail mail) {
		// TODO Auto-generated method stub
		Mail ret=null;
		try {
			qr.update("insert into mail(content) values(?)", mail.getContent());
			ret = qr.query("select * from mail where mail_id = (select max(mail_id) from mail)", new BeanHandler<>(Mail.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}
	public boolean addMailofUser(String user_id, int mail_id) {
		try {
			qr.update("insert into mail_of_user(user_id, mail_id) values (?,?)", user_id, mail_id);
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	@Override
	public boolean massMailofUser(int mail_id) {
		// TODO Auto-generated method stub
		List<User> users=null;
		try{
			users = qr.query("select * from user", new BeanListHandler<>(User.class));
		}catch(SQLException e) {
			e.printStackTrace();
		}
		if (users == null || users.size() == 0) return false;
		Object[][] obj = new Object[users.size()][2];
		for(int i=0; i<users.size(); ++i){
			obj[i][0] = users.get(i).getUser_id();
			obj[i][1] = mail_id;
		}
		
		try {
			qr.batch("insert into mail_of_user(user_id, mail_id) values (?,?)", obj);
		}catch(SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	

}




